package com.mopa.qzonegallery;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class HcSqlDataHelper {
	private static String DB_NAME = "qzgallery.db";
	private static int DB_VERSION = 2;
	private SQLiteDatabase db;
	private HcSqlHelper dbHelper;
	
	public HcSqlDataHelper(){
        dbHelper=new HcSqlHelper(DB_NAME, null, DB_VERSION);
        db= dbHelper.getWritableDatabase();
    }
	
	public void close()
	{
        db.close();
        dbHelper.close();
	}
	
	public synchronized List<String> getAllList()
	{
        String columns[]={HcSqlHelper.FILENAME};
	    List<String> filePathArray = new ArrayList<String>();
        Cursor cursor=db.query(HcSqlHelper.TB_NAME, columns, null, null, null, null, HcSqlHelper.ID+" DESC");
        cursor.moveToFirst();
        while(!cursor.isAfterLast()&& (cursor.getString(0)!=null))
        {
        	String strName=cursor.getString(0);
            filePathArray.add(strName);
            cursor.moveToNext();
        }
        cursor.close();
        return filePathArray;
	}

	 public synchronized void delItemByFileName(String fileName)
	 {
		 fileName="'"+fileName+"'";
		 db.delete(HcSqlHelper.TB_NAME,HcSqlHelper.FILENAME+"="+fileName, null);
	 }
	 
	 public synchronized void delItemByfolderName(String folderPath)
	 {
		 folderPath="'"+folderPath+"'";
		 db.delete(HcSqlHelper.TB_NAME,HcSqlHelper.FOLDERPATH+"="+folderPath, null);
	 }
	 
	 public synchronized Boolean contain(String fileName)
	   {
	       Boolean b=false;
	       fileName="'"+fileName+"'";
	       Cursor cursor=db.query(HcSqlHelper.TB_NAME, null,HcSqlHelper.FILENAME+"="+fileName, null, null, null, null);
	       b=cursor.moveToFirst();
	       cursor.close();
	       return b;
	   }
	 
	 public synchronized Long addOne(String file)
	 {
		 File picFIle=new File(file);
		 String folderPath=picFIle.getParent();
		 ContentValues values = new ContentValues();
	     values.put(HcSqlHelper.FILEPATH, file);
	     values.put(HcSqlHelper.FOLDERPATH, folderPath);
	     values.put(HcSqlHelper.FILENAME, picFIle.getName());
	     Long uid = db.insert(HcSqlHelper.TB_NAME,null,values);
	     return uid;
	 }

  }
